package com.project.basis.base;

import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.annotation.IdRes;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private ArrayMap<Integer, View> viewArrayMap;

    public BaseViewHolder(View itemView) {
        super(itemView);
        viewArrayMap = new ArrayMap<>();
    }

    public View getView(@IdRes int id) {
        View view = viewArrayMap.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
            viewArrayMap.put(id, view);
        }
        return view;
    }

    public AppCompatTextView getTextView(@IdRes int id) {
        if (getView(id) instanceof TextView) {
            return (AppCompatTextView) getView(id);
        }
        return null;
    }

    public AppCompatImageView getImageView(@IdRes int id) {
        if (getView(id) instanceof ImageView) {
            return (AppCompatImageView) getView(id);
        }
        return null;
    }

    public AppCompatCheckBox getCheckBox(@IdRes int id) {
        if (getView(id) instanceof CheckBox) {
            return (AppCompatCheckBox) getView(id);
        }
        return null;
    }

    public AppCompatButton getButton(@IdRes int id) {
        if (getView(id) instanceof Button) {
            return (AppCompatButton) getView(id);
        }
        return null;
    }

    public AppCompatEditText getEditText(@IdRes int id) {
        if (getView(id) instanceof EditText) {
            return (AppCompatEditText) getView(id);
        }
        return null;
    }

    public AppCompatRadioButton getRadioButton(@IdRes int id) {
        if (getView(id) instanceof RadioButton) {
            return (AppCompatRadioButton) getView(id);
        }
        return null;
    }

    public Group getGroup(@IdRes int id) {
        if (getView(id) instanceof Group) {
            return (Group) getView(id);
        }
        return null;
    }

    public ConstraintLayout getConstraintLayout(@IdRes int id) {
        if (getView(id) instanceof ConstraintLayout) {
            return (ConstraintLayout) getView(id);
        }
        return null;
    }

    public ViewFlipper getViewFlipper(@IdRes int id) {
        if (getView(id) instanceof ViewFlipper) {
            return (ViewFlipper) getView(id);
        }
        return null;
    }

    public TabLayout getTabLayout(@IdRes int id) {
        if (getView(id) instanceof TabLayout) {
            return (TabLayout) getView(id);
        }
        return null;
    }

    public RecyclerView getRecyclerView(@IdRes int id) {
        if (getView(id) instanceof RecyclerView) {
            return (RecyclerView) getView(id);
        }
        return null;
    }
}